using System;
using System.Collections.Generic;
using FinancialAnalyzer.ToolClasses;

namespace FinancialAnalyzer.DataBase
{
    [Serializable]
    public class Calendar : IList<KeyValuePaar<Year, Double>>
    {
        List<KeyValuePaar<Year, Double>> _yearList;


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="year"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Calendar(Year year, Double obj)
        {
            _yearList = new List<KeyValuePaar<Year, Double>>();
            _yearList.Add(new KeyValuePaar<Year, Double>(year, obj));
        }
        public Calendar()
        {
            _yearList = new List<KeyValuePaar<Year, Double>>();
        }

        public void Add(Year year, Double obj)
        {
            _yearList.Add(new KeyValuePaar<Year, Double>(year, obj));
        }

        public void Add(Year year, Month month, Double value, String comment)
        {
            int yearIndex = IndexOf(year);
            int monthIndex = IndexOf(yearIndex, month);

            _yearList[yearIndex].Key[monthIndex].Key.Add(comment, value);
        }
        
        public bool Remove(Year year)
        {
            for (int i = 0; i < _yearList.Count; i++)
            {
                if (_yearList[i].Key.Equals(year))
                {
                    _yearList.Remove(_yearList[i]);
                    return true;
                }
            }
            return false;
        }

        private int IndexOf(Year year)
        {
            for (int i = 0; i < _yearList.Count; i++)
            {
                if (_yearList[i].Key.Equals(year))
                {
                    return i;
                }
            }
            return -1;
        }

        public int IndexOf(String year)
        {
            for (int i = 0; i < _yearList.Count; i++)
            {
                if (_yearList[i].Key.ToString().Equals(year))
                {
                    return i;
                }
            }
            return -1;
        }

        public int IndexOf(int yearindex, Month month)
        {
            for (int i = 0; i < _yearList.Count; i++)
            {
                if (_yearList[yearindex].Key[i].Key.Equals(month))
                {
                    return i;
                }
            }
            return -1;
        }

        public Double Sum(Double valuevalency)
        {
            Double sum = 0;
            for (int i = 0; i < _yearList.Count; i++)
            {
                _yearList[i].Value = Math.Round(_yearList[i].Key.Sum(valuevalency),2);
                sum += _yearList[i].Value;
            }
            return sum;
        }

        public List<KeyValuePaar<String, Double>> Find(String searchString)
        {
            List<KeyValuePaar<String, Double>> tempList = new List<KeyValuePaar<String, Double>>();
            foreach (KeyValuePaar<Year, double> paar in _yearList)
            {
                tempList.AddRange(paar.Key.Find(searchString));
            }
            return tempList;
        }

       
        #region IList<KeyValuePaar<Year,Double>> Member

        public int IndexOf(KeyValuePaar<Year, Double> item)
        {
            return _yearList.IndexOf(item);
        }

        public void Insert(int index, KeyValuePaar<Year, Double> item)
        {
            _yearList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _yearList.RemoveAt(index);
        }

        public KeyValuePaar<Year, Double> this[int index]
        {
            get
            {
                return _yearList[index];
            }
            set
            {
                _yearList[index] = value;
            }
        }

        #endregion

        #region ICollection<KeyValuePaar<Year,Double>> Member

        public void Add(KeyValuePaar<Year, Double> item)
        {
            _yearList.Add(item);
        }
              

        public void Clear()
        {
            _yearList.Clear();
        }

        public bool Contains(KeyValuePaar<Year, Double> item)
        {
            return _yearList.Contains(item);
        }

        public void CopyTo(KeyValuePaar<Year, Double>[] array, int arrayIndex)
        {
            _yearList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return  _yearList.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePaar<Year, Double> item)
        {
            return _yearList.Remove(item);
        }

        

        #endregion

        #region IEnumerable<KeyValuePaar<Year,Double>> Member

        public IEnumerator<KeyValuePaar<Year, Double>> GetEnumerator()
        {
            return _yearList.GetEnumerator();
        }

        #endregion


        #region IEnumerable Member

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _yearList.GetEnumerator();
        }

        #endregion
    }
}
